package top.tupobi.zing.mvp.presenter;

import android.content.Context;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

import top.tupobi.zing.InfoRecord;
import top.tupobi.zing.base.BasePresenter;
import top.tupobi.zing.mvp.model.HttpResult;
import top.tupobi.zing.mvp.model.LoginResponseModel;
import top.tupobi.zing.mvp.view.AtyChat;
import top.tupobi.zing.mvp.view.ILoginView;
import top.tupobi.zing.network.DefaultObserver;
import top.tupobi.zing.network.HttpConfig;
import top.tupobi.zing.network.beiyong.NetWorkBeiyong;
import top.tupobi.zing.network.beiyong.RxUtils;


public class LoginPresenter extends BasePresenter<ILoginView> {

    public void register(String username, String password) {

        if (!checkInput(username, password)) {
            return;
        }

        Map<String, String> params = new HashMap<>();
        params.put("username", username);
        params.put("password", password);

        NetWorkBeiyong.request().getApi()
                .register(HttpConfig.REGISTER, params)
                .compose(RxUtils.applySchedulersLife())
                .subscribe(new DefaultObserver<HttpResult>() {
                    @Override
                    public void onSuccess(HttpResult httpResult) {
                        if (httpResult.getCode() == 0) {
                            toast("注册成功，请查看邮件提示");
                        } else {
                            toast(httpResult.getMessage());
                        }
                    }
                });
    }

    private boolean checkInput(String username, String password) {
        if (StringUtils.isEmpty(username)) {
            toast("请输入用户名！");
            return false;
        }

        if (StringUtils.isEmpty(password)) {
            toast("请输入密码！");
            return false;
        }
        return true;
    }

    public void login(final String username, String password, final Context context) {
        if (!checkInput(username, password)) {
            return;
        }
        Map<String, String> params = new HashMap<>();
        params.put("username", username);
        params.put("password", password);

        NetWorkBeiyong.request().getApi()
                .login(HttpConfig.LOGIN, params)
                .compose(RxUtils.applySchedulersLife())
                .subscribe(new DefaultObserver<HttpResult<LoginResponseModel>>() {
                    @Override
                    public void onSuccess(HttpResult<LoginResponseModel> httpResult) {
                        LogUtils.e("httpResult == " + httpResult);

                        if (httpResult.getCode() == 0) {
                            toast("登陆成功");

                            LoginResponseModel loginResponseModel = httpResult.getData();
                            LogUtils.e("loginResponseModel == " + loginResponseModel);

                            if (loginResponseModel.getUsername() != null) {
                                InfoRecord.setUsername(loginResponseModel.getUsername());
                            }
                            if (loginResponseModel.getId() != null) {
                                InfoRecord.setUserId(loginResponseModel.getId());
                            }
                            if (loginResponseModel.getExt1() != null) {
                                InfoRecord.setNickname(loginResponseModel.getExt1());
                            }
                            AtyChat.actionStart(context);
                            finishCurrentAty();
                        } else {
                            if (!StringUtils.isEmpty(httpResult.getMessage())) {
                                toast(httpResult.getMessage());
                            }
                        }
                    }
                });


//        AtyChat.actionStart(context);
//        finishCurrentAty();
    }
}
